import dd from "gdt-jsapi";
import api from "../api";
let userInfo = null
const getLocalUserInfo = async () => {
    let userInfo = {}
    try {
        const r = await dd.getStorageItem({
            name: "userInfo",
        })
        if (r) {
            try {
                userInfo = JSON.parse(r)
            } catch(e){}
        }
    } catch(e){}
    return userInfo
}
const getServerUserInfo = async () => {
    let userInfo = {}
    try {
        const data = await dd.getAuthCode({})
        if (data.code) {
            const res = await api.getUserInfo(data.code)
            if (res.data) {
                userInfo = res.data
                dd.setStorageItem({
                    name: "userInfo",
                    value: JSON.stringify(res.data)
                })
            }
        }
    } catch(e){}
    return userInfo
}
const getUserInfo = async () => {
    if (userInfo) {
        return userInfo
    }
    userInfo = await dd.getLoginUser()
    return userInfo
    // return await getServerUserInfo()
    // let userInfo = await getLocalUserInfo()
    // if (!userInfo.accountId) {
    //     userInfo = await getServerUserInfo()
    // }
    // return userInfo

}
export default {
    getLocalUserInfo,
    getServerUserInfo,
    getUserInfo,
}
